package com.gbcom.autocompile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.gbcom.autocompile.mail.MailNotify;
import com.gbcom.autocompile.prop.SysProperies;
import com.gbcom.op.scheduler.execute.AbstractStatelessJob;
import com.gbcom.op.scheduler.execute.JobExeContext;
import com.gbcom.op.scheduler.util.JobException;
import com.gbcom.op.util.StringUtils;

/**
 * 自动任务 自动编译 并发送邮件。。。
 * 
 * @author Administrator
 * 
 */
public class BatRunJob extends AbstractStatelessJob {
	private static final Logger logger = Logger.getLogger(BatRunJob.class);
	public static final String[] toMailers = new String[] {
			"assertmyself@163.com", "sunyanzheng@gbcom.com.cn"/*
															 * ,
															 * "doujun@gbcom.com.cn"
															 * ,
															 * "qinyeju@gbcom.com.cn"
															 */};

	@Override
	public void execute(JobExeContext jobexecontext) throws JobException {
		doTask();
	}

	public void doTask() {
		String cmd = "cmd /c " + SysProperies.getValue("base.dir") + "&cd "
				+ SysProperies.getValue("base.path") + " & "
				+ "compiler-ant.bat > build-" + getTodayFileName() + ".txt";// pass
		String restart = "cmd /c " + SysProperies.getValue("base.dir") + "&cd "
				+ SysProperies.getValue("base.path") + " & "
				+ "run-ant.bat>logs/run-" + getTodayFileName() + ".log";// pass
		if (AutoCompile.compiler_method == 2) {
			cmd = "cmd /c " + SysProperies.getValue("base.dir") + "&cd "
					+ SysProperies.getValue("base.path") + " & "
					+ "compiler-mvn.bat > build-" + getTodayFileName() + ".txt";// pass
			restart = "cmd /c " + SysProperies.getValue("base.dir") + "&cd "
					+ SysProperies.getValue("base.path") + " & "
					+ "run-mvn.bat >logs/run-" + getTodayFileName() + ".log";// pass
		}
		logger.info("String the BatRunJob ~~~~~ time=" + new Date());

		try {
			logger.info(" the BatRunJob [ step --1--] ~~ CMD: " + cmd);
			Process ps = Runtime.getRuntime().exec(cmd);
			ps.waitFor();

			// Thread.sleep(3*60*1000);
			logger.info(" the BatRunJob [step --2--]~~ CMD: " + restart);

			// 重启tomcat
			ps = Runtime.getRuntime().exec(restart);
			// ps.waitFor();
			Thread.sleep(3 * 60 * 1000);
			// 取得filelist
			Vector<String> fileList = new Vector<String>();
			fileList.add(SysProperies.getValue("base.path") + "\\build-"
					+ getTodayFileName() + ".txt");
			// fileList.add(SysProperies.getValue("base.path")+"\\target\\reports.zip");

			String[] toMailers = SysProperies.getValue("mail.to").split("\\|");
			logger.info(" the BatRunJob [step --3--]~~ ;; Send-mail >> sender:"
					+ SysProperies.getValue("mail.from.addr") + ";to:"
					+ StringUtils.arrayToDelimitedString(toMailers, "|"));

			MailNotify.sendSmtpEmail(
					SysProperies.getValue("mail.from.addr"),
					SysProperies.getValue("mail.from.user"),
					"Sunday" + SysProperies.getValue("mail.from.pwd"),
					SysProperies.getValue("mail.server"),
					toMailers,
					"版本构建",
					getEmailContent(),fileList);
		} catch (IOException e) {
			logger.info("job failed!!!" + e);
		} catch (Exception e) {
			logger.info("job failed!!!" + e);
		}
		logger.info("end the BatRunJob ~~~~~ time=" + new Date());


	}
	private static String getEmailContent(){
		String rst = "--    每日构建      --  time [ "+getTodayFileName()+"]  --- \n--\n--     构建结果 ： 详细结果见附件! file：【build-"+getTodayFileName()+".txt】   \n--\n--    版本路径(user/pwd=administrator/sunday)  : "
				+ SysProperies.getValue("cl.report.share")
				+ " \n--\n"
				+ "--   访问地址 (user/pwd=admin/admin):  "
				+ SysProperies.getValue("cl.web.url")
				+ " \n-- --    --   END    ------\n";
			
		if(SysProperies.getValue("mail.content.style")!=null && SysProperies.getValue("mail.content.style").equalsIgnoreCase("html")){
			rst = "--    每日构建      --  time [  "+getTodayFileName()+"]  --- </br>--</br>--     构建结果 ： 详细结果见附件! file：【build-"+getTodayFileName()+".txt】   </br>--</br>--    版本路径  : <a href=\""
					+ SysProperies.getValue("cl.report.share")
					+ "\"> "
					+ SysProperies.getValue("cl.report.share")
					+ "</a>(user/pwd=administrator/sunday) </br>--</br>"
					+ "--   访问地址 :  <a href=\""
					+ SysProperies.getValue("cl.web.url")
					+ "\" >"
					+ SysProperies.getValue("cl.web.url")
					+ "</a>(user/pwd=admin/admin) </br>-- --    --   END    ------</br>";
		}
		return rst;
	}
	/**
	 * 得到今天的日志文件
	 * 
	 * @return
	 */
	private static String getTodayFileName() {
		// build-2011-07-08.txt
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String destFile = dateFormat.format(new Date());
		return destFile;
	}
}
